package com.xing.crm.web.controller;

import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProProductPoJo;
import com.xing.crm.pojo.ProSkuPoJo;
import com.xing.crm.pojo.Result;
import com.xing.crm.service.ProProductService;
import com.xing.crm.service.ProSkuService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping({"/sku"})
public class ProSkuController {
	@Resource
	private ProSkuService proSkuService;
	@Resource
	private ProProductService productService;
	//前缀处理
	@Value("${spring.mvc.static-path-pattern}")
	private String imgUriPrefix;
	
	private String pathSub() {
		if(imgUriPrefix != null) {
			return imgUriPrefix.replace("/**", "");
		}
		return "";
	}
	///////////////////////////////////////////////页面////////////////////////////////////////////////////
	///////////////////////////////////////////////页面////////////////////////////////////////////////////
	///////////////////////////////////////////////页面////////////////////////////////////////////////////
	//列表页面
	@RequestMapping({"/listPage"})
	public String listPage(Model model,Integer pid){
		//注入商品信息
		if( pid != null){
			ProProductPoJo product = productService.detail(pid);
			model.addAttribute("product", product);
		}
		model.addAttribute("pid", pid);
		return "admin/product/sku_list";
	}
	
	//创建、编辑页面
	@RequestMapping({"/editPage"})
	public String editPage(Model model, Integer id,Integer pid){
		//如果有pid就是新增
		if( pid != null){
			ProProductPoJo product = productService.detail(id);
			model.addAttribute("model","add");
			//model.addAttribute("product", product);
		}
		//如果有id就是编辑
		if( id != null){
			ProSkuPoJo sku = proSkuService.detail(id);
			model.addAttribute("model","edit");
			model.addAttribute("data", sku);
		}
		model.addAttribute("pid", pid);
		model.addAttribute("uriPrefix", pathSub());
		return "admin/product/sku_edit";
	}
	
	
	///////////////////////////////////////////////数据////////////////////////////////////////////////////
	///////////////////////////////////////////////数据////////////////////////////////////////////////////
	///////////////////////////////////////////////数据////////////////////////////////////////////////////
	//数据列表
	@ResponseBody
	@RequestMapping({"/list"})
	public PageResult<ProSkuPoJo> list(
			ProSkuPoJo skuPoJo,
			@RequestParam(required = false,defaultValue = "1") Integer page,
			@RequestParam(required = false,defaultValue = "10") Integer limit
	){
		return this.proSkuService.list(skuPoJo, page, limit);
	}

	
	//新增or修改
	@ResponseBody
	@RequestMapping({"/edit"})
	public Map<String,Object> edit(ProSkuPoJo  proSkuPoJo){
		return this.proSkuService.edit(proSkuPoJo);
	}
	
	//删除
	@ResponseBody
	@RequestMapping({"/delete"})
	public Map<String,Object> delete(Integer  id){
		return this.proSkuService.delete(id);
	}
	
	//排序
	@ResponseBody
	@RequestMapping({"/sort"})
	public Result sort(ProSkuPoJo proSkuPoJo){
		return proSkuService.sort(proSkuPoJo);
	}
}
